<?php
class GoodsModel extends Model {
	protected $tableName='goods';
	public $insertID;
	
	/**
	 * 查找goods表数据，且关联category表和brand表
	 *
	 * @return array
	 */
	public function selGoods($id=''){
		if($id==''){
			$sql='select g.id,g.gid,c.name as cname,b.name as bname,g.name,g.brief,g.market,g.price,g.score,g.photo,g.sales,g.ctime from goods as g,brand as b,category as c where g.cid=c.id and g.bid=b.id';
		}else{
			$sql='select g.id,g.gid,c.name as cname,b.name as bname,g.name,g.brief,g.market,g.price,g.score,g.photo,g.sales,g.ctime from goods as g,brand as b,category as c where g.cid=c.id and g.bid=b.id and g.id='.$id;
		}
		$res=mysql_query($sql) or die(mysql_error());
		$goodsData=array();
		while ($row=mysql_fetch_assoc($res)) {
			$goodsData[]=$row;
		}
		if (count($goodsData)==1) {
			return $goodsData[0];
		}else{
			return $goodsData;
		}
	}
	
	/**
	 * 前台取热卖商品
	 */
	public function hotgoods(){
		$sql='select * from '.$this->tableName.' order by sales desc limit 9';
		$res=mysql_query($sql);
		$goodsData=array();
		while ($row=mysql_fetch_assoc($res)) {
			$goodsData[]=$row;
		}
		return $goodsData;
	}
	
	public function goodslist($flag='ctime',$sort='desc'){
		$sql='select * from '.$this->tableName.' order by '.$flag.' '.$sort.' limit 12';
		$res=mysql_query($sql);
		$goodsData=array();
		while ($row=mysql_fetch_assoc($res)) {
			$goodsData[]=$row;
		}
		return $goodsData;
	}
	
	public function insertID($array){
		$goods= new GoodsModel();
		$num=$goods->insert($array);
		$this->insertID=$this->_db->insert_id();
		return $num;
	}
}
?>